package com.maszy.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.maszy.bean.Car;
import com.maszy.bean.Dept;
import com.maszy.bean.DoorStore;
import com.maszy.bean.GoodPlan;
import com.maszy.bean.Breakfast;
import com.maszy.service.CarService;
import com.maszy.service.DeptService;
import com.maszy.service.BreakfastService;

public class BreakfastServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	BreakfastService service = new BreakfastService();
	String view = new String(); // 页面路径

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		BreakfastServlet servlet = new BreakfastServlet();
		RequestDispatcher rd = null;
		String method = request.getParameter("method");
	if ("queryAll".equals(method)) {
			// 查询出全部数据
			servlet.queryAll(request, response);
			view = "/background/breakfast/breakfast.jsp";
		} else if ("insert".equals(method)) {
			// 添加新记录
			servlet.add(request, response);
			servlet.queryAll(request, response);
			view = "/background/breakfast/breakfast.jsp";
		} else if ("update".equals(method)) {
			// 修改记录
			servlet.update(request, response);
			servlet.queryAll(request, response);
			view = "/background/breakfast/breakfast.jsp";
		//view = "/background/breakfast?method=updateForward&Action_Id=902";
		} else if ("delete".equals(method)) {
			// 删除记录
			servlet.delete(request, response);
			servlet.queryAll(request, response);
		} else if ("updateForward".equals(method)) {
			// 进入修改页面
			servlet.queryForOne(request, response);
			view = "/background/breakfast/update_breakfast.jsp";
		} else if ("insertForward".equals(method)) {
			// 进入添加页面
			view = "/background/breakfast/update_breakfast.jsp";
		} 

		try {
			rd = this.getServletContext().getRequestDispatcher(view);
			rd.forward(request, response);

		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String id=request.getParameter("breakfast_Id");
		if(!"".equals(id)&&id!=null){
			Breakfast obj=new Breakfast();
			obj.setBreakfast_id(Long.parseLong(id));
			service.delete(obj);
		}
	}

	private void queryForOne(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		String id=request.getParameter("breakfast_Id");
		if(!"".equals(id)&&id!=null){
			Breakfast obj=new Breakfast();
			obj.setBreakfast_id(Long.parseLong(id));
			obj=service.queryById(obj);
			request.setAttribute("obj", obj);
		}
		
//		DeptService deptService= new DeptService();
//		List list=deptService.queryAll();
//		request.setAttribute("list", list);
	}

	private void update(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		Breakfast obj=new Breakfast();
		String id=request.getParameter("breakfast_Id");
		obj.setProduct_name(request.getParameter("Product_Name"));
		obj.setSpecification(request.getParameter("Breakfast_Specification"));
		obj.setUnit(request.getParameter("Breakfast_Unit"));
		obj.setDept_id(1);
		obj.setStatus(request.getParameter("Breakfast_Status"));
		obj.setBreakfast_Sort(Integer.valueOf(request.getParameter("Breakfast_Sort")));
		if(request.getParameter("Breakfast_Morning")!=null){
			obj.setMorning_tang(Integer.parseInt(request.getParameter("Breakfast_Morning")));
		}
		if(request.getParameter("Afternoon_tang")!=null){
			obj.setAfternoon_tang(Integer.parseInt(request.getParameter("Afternoon_tang")));
		}
		obj.setRemark(request.getParameter("Breakfast_Remark"));
		if(id!=null&&!"".equals(id)){	
			obj.setBreakfast_id(Long.parseLong(id));
			service.update(obj);
		}else{
			service.add(obj);
		}
	}

	private void queryAll(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
//		List list = service.queryAll();
//		request.setAttribute("list", list);
//		DeptService deptService=new DeptService();
//		List<Dept> deptList=deptService.queryAll();
//		List<GoodPlan> goodPlanList=new ArrayList<GoodPlan>();	
		List<Breakfast> proList=service.queryAll();
		/*for(int i=0;i<deptList.size();i++){
			int dept_id=deptList.get(i).getDept_id();
			List<Breakfast> tempList=new ArrayList<Breakfast>();
			for(int j=0;j<proList.size();j++){
				if(dept_id==proList.get(j).getBreakfast_Dept_Id()){
					tempList.add(proList.get(j));
				}
			}
			GoodPlan goodPlan=new GoodPlan();
			goodPlan.setDept_Name(deptList.get(i).getDept_name());
			goodPlan.setBreakfastList(tempList);
			goodPlanList.add(goodPlan);
		}*/
		request.setAttribute("list", proList);
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		Breakfast obj = new Breakfast();
		obj.setProduct_name(request.getParameter("Breakfast_Name"));
		obj.setSpecification(request.getParameter("Breakfast_Specification"));
		obj.setUnit(request.getParameter("Breakfast_Unit"));
		obj.setDept_id(1);
		obj.setStatus(request.getParameter("Breakfast_Status"));
		int breakfast=Integer.valueOf(request.getParameter("Breakfast_Sort"));
		obj.setBreakfast_Sort(breakfast);
		
		if(request.getParameter("Breakfast_Morning")!=null){
			obj.setMorning_tang(Integer.parseInt(request.getParameter("Breakfast_Morning")));
		}
		if(request.getParameter("Breakfast_Afternoon")!=null){
			obj.setAfternoon_tang(Integer.parseInt(request.getParameter("Afternoon_tang")));
		}
		obj.setRemark(request.getParameter("Breakfast_Remark"));
		service.add(obj);	
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		doGet(request, response);
	}
}